<cftry>
<cfparam name="attributes.index" type="string">
<cfparam name="attributes.term" type="string">
<cfparam name="attributes.result" type="variableName" default="result">
<cfparam name="attributes.fields" type="string">

<!--- 
This default root is used in cases where indexdirectory is a simple value, not
a full path.
--->
<cfset defaultroot = "#server.coldfusion.rootdir#/luceneindex">

<cfif not find("\", attributes.index) and not find("/", attributes.index)>
	<cfset attributes.index = defaultroot & "/" & attributes.index>
</cfif>

<!--- create indexdirectory if it doesn't exist --->
<cfif not directoryExists(attributes.index)>
	<cfthrow message="Index [#attributes.index#] does not exist.">
</cfif>


<cfset indexReader = createObject("java", "org.apache.lucene.index.IndexReader")>
<cfset indexReader.open(attributes.index)>

<cfset searcher = createObject("java", "org.apache.lucene.search.IndexSearcher")>
<cfset searcher.init(attributes.index)>

<cfset analyzer = createObject("java", "org.apache.lucene.analysis.standard.StandardAnalyzer").init() />

<cfset fieldArray = ArrayNew(1) />
<cfset fieldArray = ListToArray(lcase(attributes.fields))>
<!--- <cfdump var="#fieldArray#" label="fieldArray"> --->

<cfset termArray = ArrayNew(1) />
<cfloop list="#lcase(attributes.fields)#" index="field">
<cfset ArrayAppend(termArray, '#attributes.term#')>
</cfloop>
<!--- <cfset ArrayAppend(termArray,attributes.term) /> --->
<!--- <cfdump var="#termArray#" label="termArray"> --->

<cfset parser = createObject("java", "org.apache.lucene.queryParser.MultiFieldQueryParser") />
<cfset parser.init(fieldArray, analyzer) />
<!--- String[] fields, Analyzer analyzer --->
<!--- <cfdump var="#parser#" label="parser"> --->

<cfset query = parser.parse(termArray, fieldArray, analyzer) />
<!-- parse(String[] queries, String[] fields, Analyzer analyzer)  --->
<!--- <cfdump var="#query#" label="query" /> --->

<cfset hits = searcher.search(query)>

<cfset results = queryNew("score")>

<cfif hits.length() gt 0>
	<cfloop index="x" from="1" to="#hits.length()#">
		<cfset hit = hits.doc(x-1)>
		<cfset fields = hit.getFields()>
		<cfset queryAddRow(results)>

		<cfloop index="y" from="1" to="#arrayLen(fields)#">
			<cfset field = fields[y]>
			<!--- build the query up manually --->
			<cfif not listFindNoCase(results.columnlist, field.name())>
				<cfset queryAddColumn(results, field.name(), arrayNew(1))>
			</cfif>
			<cfset querySetCell(results, field.name(), hit.get(field.name()))>
		</cfloop>

		<cfset querySetCell(results, "score", hits.score(x-1))>

	</cfloop>
</cfif>	

<cfset caller[attributes.result] = results>

<cfcatch>
<cfdump var="#CFCATCH#" label="error">
<cfabort />
</cfcatch>
</cftry>